namespace SilverSea.Untilities
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Timers;

    public class NetworkUltilizationMonitor
    {
        private ArrayList arrayList_0 = new ArrayList();
        private ArrayList arrayList_1 = new ArrayList();
        private bool bool_0;
        private Timer timer_0;

        public NetworkUltilizationMonitor()
        {
            this.method_0();
            this.timer_0 = new Timer(1000.0);
            this.timer_0.Elapsed += new ElapsedEventHandler(this.timer_0_Elapsed);
        }

        private void method_0()
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
            foreach (string str in category.GetInstanceNames())
            {
                if (!(str == "MS TCP Loopback interface"))
                {
                    NetworkAdapter adapter = new NetworkAdapter(str) {
                        performanceCounter_0 = new PerformanceCounter("Network Interface", "Bytes Received/sec", str),
                        performanceCounter_1 = new PerformanceCounter("Network Interface", "Bytes Sent/sec", str)
                    };
                    this.arrayList_0.Add(adapter);
                }
            }
        }

        public void StartMonitoring()
        {
            if (this.arrayList_0.Count > 0)
            {
                foreach (NetworkAdapter adapter in this.arrayList_0)
                {
                    if (!this.arrayList_1.Contains(adapter))
                    {
                        this.arrayList_1.Add(adapter);
                        adapter.method_0();
                    }
                }
                this.timer_0.Enabled = true;
                this.bool_0 = true;
            }
        }

        public void StartMonitoring(NetworkAdapter adapter)
        {
            if (!this.arrayList_1.Contains(adapter))
            {
                this.arrayList_1.Add(adapter);
                adapter.method_0();
            }
            this.timer_0.Enabled = true;
            this.bool_0 = true;
        }

        public void StopMonitoring()
        {
            this.bool_0 = false;
            this.timer_0.Enabled = false;
            this.arrayList_1.Clear();
        }

        public void StopMonitoring(NetworkAdapter adapter)
        {
            this.bool_0 = false;
            this.timer_0.Enabled = false;
            if (this.arrayList_1.Contains(adapter))
            {
                this.arrayList_1.Remove(adapter);
            }
        }

        private void timer_0_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                foreach (NetworkAdapter adapter in this.arrayList_1)
                {
                    adapter.method_1();
                }
            }
            catch (Exception)
            {
            }
        }

        public NetworkAdapter[] Adapters
        {
            get
            {
                return (NetworkAdapter[]) this.arrayList_0.ToArray(typeof(NetworkAdapter));
            }
        }

        public bool IsRunning
        {
            get
            {
                return this.bool_0;
            }
        }
    }
}

